import random
import re
import time

import mitmproxy.http

from sam import import_csv_path
from sam.util.fileUtil2 import read_file, write_file_quick
from sam.util.httpUtil import BaseHttp, get_url_encode_str
from sam.util.strUtil import extract_number


class RedBook(object):
    def __init__(self):
        super().__init__()
        self.http = BaseHttp()
        self.user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat"

    def fetch_topic_notes_num_by_keyword(self, keyword) -> int:

        def request(flow: mitmproxy.http.HTTPFlow):
            if 'https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/search/notes' in flow.request.url:
                authorization = re.findall("authorization',.*?'(.*?)'\)", str(flow.request.headers))[0]
                x_sign = re.findall("x-sign',.*?'(.*?)'\)", str(flow.request.headers))[0]

        keyword_str = get_url_encode_str(keyword)
        search_url = f"https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/search/recommend?keyword={keyword_str}"
        resp_json = self.http.get(
            url=search_url
            , resp_optional="json"
            , header={
                "Host": "www.xiaohongshu.com",
                "Connection": "keep-alive",
                "Authorization": "",
                "Device-Fingerprint": "WHJMrwNw1k/EYhSDmj33uW5ACUTT+J2IepImw4wvmtLVuHrfsxId435AFCerTiU2tewue9GBfE5Iz7NtB7aCPxzlwFK/E+h82dCW1tldyDzmauSxIJm5Txg==1487582755342",
                "User-Agent": self.user_agent,
                "X-Sign": "X5499d155d5267fd1c53418f0cab1624d",
                "content-type": "application/json",
                "Referer": "https://servicewechat.com/wxb296433268a1c654/46/page-frame.html",
                "Accept-Encoding": "gzip, deflate, br"
            }
        )
        if resp_json and resp_json['code'] == 0:
            search_desc = resp_json["data"][0]['desc']
            replace = extract_number(search_desc)
            return replace
        else:
            pass

    def main(self):
        line_list = read_file(import_csv_path)
        data_list = [['品牌名称', '相关笔记篇数']]
        for line in line_list:
            keyword = line[0].strip()
            if keyword:
                time.sleep(random.randint(5, 8))
                print(f"将要抓取  关键词： {keyword}")
                topic_notes_num = self.fetch_topic_notes_num_by_keyword(keyword)
                if topic_notes_num:
                    data_list.append([keyword, int(topic_notes_num)])
                else:
                    data_list.append([keyword, 0])
        else:
            print("全部已抓取完毕")
            write_file_quick(data_list=data_list, file_type="xlsx", export_file_name="雪糕")
            # work_we_chart_notify(f" 当前 批次的 关键词 爬取完了, {line_list[0][0]} -- {line_list[-1][0]}")

    def main2(self):
        keyword = "芊动"
        print(f"将要抓取  关键词： {keyword}")
        by_keyword = self.fetch_topic_notes_num_by_keyword(keyword)
        print(by_keyword)


def red_book():
    process = RedBook()
    process.main()


if __name__ == "__main__":
    red_book()
